/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client.view;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.AbstractHtmlLayoutContainer.HtmlData;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.Viewport;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.srv.client.presenter.Display;
import com.srv.shared.LoginInfo;

public class BaseView extends Viewport implements Display {

	private final InlineHyperlink homeLink;
	private final InlineHyperlink designLink;
	private final InlineHyperlink sessionLink;
	private final InlineHyperlink runLink;
	private HTML signOutHtml;

	public interface HtmlLayoutContainerTemplate extends XTemplates {
		    @XTemplate("<table border=0 height=40px width=100%><tbody><tr>" +
		    		"<td style=\"text-align:center;min-width:200px;\"><span style=\"font-family:Verdana;font-size:150%;\">OneOrTwo</span></td>" +
		    		"<td style=\"padding:10px 5px 5px 5px;\" class=\"cell1\" />" +
		    		"<td style=\"padding:10px 5px 5px 5px;\" class=\"cell2\" />" +
		    		"<td style=\"padding:10px 5px 5px 5px;\" class=\"cell3\" />" +
		    		"<td style=\"padding:10px 5px 5px 5px;\" class=\"cell4\" />" +
		    		"<td width=80% align=right valign=middle><span class=\"cell5\"/></td>" +
		    		"</tr></tbody></table>")
		    SafeHtml getTemplate();
	}
	
	private HtmlLayoutContainer html;
	
	protected ContentPanel setListPanel;
	protected ContentPanel mainPanel;
	protected ContentPanel toolsPanel;
	
	public BaseView(final String token, final LoginInfo loginInfo) {

	    homeLink = new InlineHyperlink("Home", "home") {{
	    	if (token.equals("home"))
    			setStyleName("mainmenu-selected");
	    	else
		    	setStyleName("mainmenu");
	    }};
	    designLink = new InlineHyperlink("Studio", "studio") {{
	    	if (token.equals("studio"))
    			setStyleName("mainmenu-selected");
	    	else
		    	setStyleName("mainmenu");
	    }};
	    sessionLink = new InlineHyperlink("Session", "session") {{
	    	if (token.equals("session"))
    			setStyleName("mainmenu-selected");
	    	else
		    	setStyleName("mainmenu");
	    }};
	    runLink = new InlineHyperlink("Run", "run") {{
	    	if (token.equals("run"))
    			setStyleName("mainmenu-selected");
	    	else
		    	setStyleName("mainmenu");
	    }};
		
	    add(new BorderLayoutContainer() {{ 
	    	setBorders(false);
		    setNorthWidget(createNorthWidget(loginInfo), new BorderLayoutData(40) {{
				setMargins(new Margins(0, 5, 5, 5));
			}});
		    setWestWidget(createWestWidget(), new BorderLayoutData(250) {{
				setMargins(new Margins(0,5,0,5));
				setCollapsible(true);  
				setSplit(true);  
		    }});
		    setCenterWidget(createCenterWidget(), new MarginData() {{
				setMargins(new Margins(0,5,0,0));
		    }});
		    setSouthWidget(createSouthWidget(), new BorderLayoutData(20) {{
				setMargins(new Margins(0,5,0,5));
				setCollapsible(false);  
				setSplit(false);  
		    }});
	    }}); 
	}

	private HtmlLayoutContainer createNorthWidget(LoginInfo loginInfo) {
	    
	    signOutHtml = (loginInfo==null)? 
	    		new HTML("<span style=\"font-family:Verdana;font-size:100%;\">" +
	    		"<a href=\"#design\">sing in</a></span>") //TODO
	    		:
	    		new HTML("<span style=\"font-family:Verdana;font-size:100%;\">"+loginInfo.getNickname()+" | " +
	    		"<a href=\""+loginInfo.getLogoutUrl()+"\">sing out</a></span>");
	    
   		html = new HtmlLayoutContainer(
   	    		((HtmlLayoutContainerTemplate)(GWT.create(HtmlLayoutContainerTemplate.class))).getTemplate());
	    html.add(homeLink, new HtmlData(".cell1"));
	    html.add(designLink, new HtmlData(".cell2"));
	    html.add(sessionLink, new HtmlData(".cell3"));		
	    html.add(runLink, new HtmlData(".cell4"));
	    html.add(signOutHtml, new HtmlData(".cell5"));
	    
		return html;
	}
	
	@Override
	public void onLogin(LoginInfo loginInfo) {
	    signOutHtml.setHTML("<span style=\"font-family:Verdana;font-size:100%;\">"+loginInfo.getNickname()+" | " +
	    		"<a href=\""+loginInfo.getLogoutUrl()+"\">sing out</a></span>");
	}
	
	private ContentPanel createWestWidget() {
    	setListPanel = new ContentPanel(){{
				setBodyBorder(true);
				setHeadingText("Set List");
		    }};
	    
	    return setListPanel;
	}

	private ContentPanel createCenterWidget() {	
		mainPanel = new ContentPanel(){{
			setBodyBorder(true);
			setHeaderVisible(false);
	    }};
	    
		toolsPanel = new ContentPanel(){{
			setHeadingText("Components");
	    }};		
	    
	    return new ContentPanel() {{
		    add(new BorderLayoutContainer() {{
			    setCenterWidget(mainPanel, new MarginData() {{
					setMargins(new Margins(0,0,0,0));
			    }});
			    setEastWidget(toolsPanel, new BorderLayoutData(250) {{
					setMargins(new Margins(0,0,0,5));
					setCollapsible(true);  
					setSplit(true);  
			    }});		    	
//			    collapse(LayoutRegion.EAST);
	    	}});	
		    
			getHeader().addTool(new ToolButton(ToolButton.GEAR, new SelectHandler() {
				@Override
				public void onSelect(SelectEvent event) {
					//final SetDesignPortal setDesignPortal = (SetDesignPortal) mainPanel.getWidget(0);
				}
			}));			
		    
	    }};
	}
	
	private HtmlLayoutContainer createSouthWidget() {
		return new HtmlLayoutContainer("<span style='font-family:Verdana;font-size:80%;'>Copyright &copy; 2012 Vadim Surov</span>");
	}		
	
	public HasClickHandlers getHomeButton() {
		return homeLink;
	}

	public HasClickHandlers getDesignButton() {
		return designLink;
	}

	public HasClickHandlers getSessionButton() {
		return sessionLink;
	}

	public HasClickHandlers getRunButton() {
		return runLink;
	}

	public Widget asWidget() {
		return this;
	}

}
